package com.sunny.blog.service.impl;

import com.sunny.blog.service.ICacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: CacheServiceImpl
 * @Description: 缓存服务实现
 * @Author: sunt
 * @Date: 2019/11/15 20:21
 * @Version 1.0
 **/
@Service
public class CacheServiceImpl implements ICacheService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String getCacheInfoByCode(String verifyCode) {
        return (String) redisTemplate.opsForValue().get(verifyCode);
    }

    @Override
    public void cacheStringData(String cacheKey,String cacheValue,long timeOut) {
        redisTemplate.opsForValue().set(cacheKey, cacheValue,timeOut, TimeUnit.MINUTES);//分钟过期
    }

    @Override
    public Boolean deleteCacheByKey(String cacheKey) {
        return redisTemplate.delete(cacheKey);
    }

    @Override
    public Long deleteBatchCache(String cacheKey) {
        Set<String> cacheKeys = redisTemplate.keys(cacheKey + "*");
        return redisTemplate.delete(cacheKeys);
    }

    @Override
    public void cacheObjData(String cacheKey, Object cacheValue, long timeout) {
        redisTemplate.opsForValue().set(cacheKey, cacheValue, timeout, TimeUnit.MINUTES);
    }

    @Override
    public void cacheObjData(String cacheKey, Object cacheValue) {
        redisTemplate.opsForValue().set(cacheKey, cacheValue);
    }

    @Override
    public Object getObjCacheByCode(String cacheKey) {
        return redisTemplate.opsForValue().get(cacheKey);
    }

    @Override
    public void lPush(String k, Object object) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.leftPushAll(k,object);
    }

    @Override
    public Object lPop(String k) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.leftPop(k);
    }

    @Override
    public List<Object> lRange(String k, long start, long end) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k,start,end);
    }

    @Override
    public void zAdd(String key, Object member, double score) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key,member,score);
    }

    @Override
    public Set<Object> zRange(String key, long start, long end) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.range(key, start, end);
    }

    @Override
    public boolean setExpire(String key, long timeout) {
        return redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    @Override
    public Long zrem(String key, Object... values) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        return zSet.remove(key,values);
    }

    @Override
    public Long incr(String key) {
        return redisTemplate.opsForValue().increment(key);
    }

    @Override
    public Long flushAll() {
        Set<String> keys = stringRedisTemplate.keys("*");
        return stringRedisTemplate.delete(keys);
    }

    @Override
    public Set<String> getCacheInfo() {
        Set<String> keys = stringRedisTemplate.keys("*");
        return keys;
    }
}
